<?php
class slideshow_model extends MY_model
{
    public $id;
    public $title_en;
    public $title_vi;
    public $description_en;
    public $description_vi;
    public $image_path;
    public $modified_date;

    public function __construct()
    {
        parent::__construct();
        $this->table_name = 'slideshow';
    }

    public static function get_elements()
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM slideshow ORDER BY modified_date DESC");
        return self::to_domain_models($query->result());
    }

    public static function get_first_element()
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM slideshow ORDER BY modified_date DESC LIMIT 0,1");
        return self::to_domain_model($query->row());
    }

    public static function get_element_by_id($id)
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM slideshow WHERE id = ?",array($id));
        return self::to_domain_model($query->row());
    }

    // -------------------------------------------------------------------------------- PRIVATE METHOD

    // Private method
    private static function to_domain_models($array)
    {
        $result = array();
        foreach($array as $item)
        {
            $result[] = slideshow_model::to_domain_model($item);
        }
        return $result;
    }

    // Private method
    private static function to_domain_model($item)
    {
        $model = new slideshow_model();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->title_en = isset($item->title_en) ? $item->title_en : '' ;
        $model->title_vi = isset($item->title_vi) ? $item->title_vi : '' ;
        $model->description_en = isset($item->description_en) ? $item->description_en : '';
        $model->description_vi = isset($item->description_vi) ? $item->description_vi : '';
        $model->image_path = isset($item->image_path) ? $item->image_path: '';
        $model->modified_date = isset($item->modified_date) ? $item->modified_date: '0';
        return $model;
    }

    protected function to_array()
    {
        $data_arr = array(
            'title_en' => $this->title_en,
            'title_vi' => $this->title_vi,
            'description_en' => $this->description_en,
            'description_vi' => $this->description_vi,
            'image_path' => $this->image_path,
            'modified_date' => $this->modified_date
        );

        return $data_arr;
    }
}